// pages/switchCity/switchCity.js
// var cityObj = require('../mock/cityData.js').cityObj;
// var city = require('../../utils/city.js');
var app = getApp()
Page({
	data: {
		cityList: [],//城市数据
		hotcityList: [],//热门城市
		searchLetter: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
		winHeight: 0,//屏幕高
		scrollTop: 0,//置顶高度
		showLetter: "",//显示选中的字母
		isShowLetter: false,//显示选中
		scrollTopId: '',//置顶id 字母
		city: "全国",//当前城市
		isIndex:false,//是不是首页来的
	},
	onLoad: function (options) {

		//配置字母表数据
		var searchLetter = this.data.searchLetter;
		var winHeight = app.globalData.systemInfo.windowHeight;
		var itemH = winHeight / searchLetter.length;
		var tempObj = [];
		for (var i = 0; i < searchLetter.length; i++) {
			var temp = {};
			temp.name = searchLetter[i];
			temp.tHeight = i * itemH;
			temp.bHeight = (i + 1) * itemH;
			tempObj.push(temp)
		}
		this.setData({
			winHeight: winHeight,
			searchLetter: tempObj,
			city: options.city,
      isIndex:options.isindex?true:false
		})
		//获取城市数据
		this.getCitys();
	},
	//获取城市数据
	getCitys: function () {
		var that = this;
		app.apiGet(app.apiList.citys, {}, function (res) {
			if (res.code == 200) {
				//对城市信息进行分组
				var tempObj = [];
				var searchLetter = that.data.searchLetter;
				var cityObj = res.data;
				for (var i = 0; i < searchLetter.length; i++) {
					var initial = searchLetter[i].name;
					var cityInfo = [];
					var tempArr = {};
					tempArr.initial = initial;
					for (var j = 0; j < cityObj.length; j++) {
						if (initial == cityObj[j].initial) {
							cityInfo.push(cityObj[j]);
						}
					}
					tempArr.cityInfo = cityInfo;
					tempObj.push(tempArr);
				}
				//热门城市 1否，2是
				var hotcityList=[];
				for (var h = 0; h < cityObj.length; h++) {
					if (cityObj[h].is_hot ==2) {
						hotcityList.push(cityObj[h]);
					}
				}
				hotcityList.unshift({
					id: '0',
					city: '全国',
					provincecode:'全国',
					code:'quanguo',
					is_hot:2,
					initial:'Q'
				});
				that.setData({
					cityList: tempObj,
					hotcityList: hotcityList
				})

			}
		})
	},
	//选择字母
	clickLetter: function (e) {
		var showLetter = e.currentTarget.dataset.letter;
		this.setData({
			showLetter: showLetter,
			isShowLetter: true,
			scrollTopId: showLetter,
		})
		var that = this;
		setTimeout(function () {
			that.setData({
				isShowLetter: false
			})
		}, 1000)
	},
	//选择城市
	bindCity: function (e) {
		this.setData({
			city: e.currentTarget.dataset.city
		})
		//更新搜索城市
		var pages = getCurrentPages();
		var curPage = pages[pages.length - 2];
		curPage.setData({
			city: e.currentTarget.dataset.city
		});
    //如果是首页来的，要更新一下页面数据
    if (this.data.isIndex){
      curPage.setData({
        nextPageUrl: true,
        pageNum: 1,
        positions: [],
        needRec:true
      });
      curPage.getIndexPositions(e.currentTarget.dataset.city,1,function(){})
    }
    
		setTimeout(function () {
			wx.navigateBack({

			})
		}, 500)
	},
	//点击当前热门回到顶部
	hotCity: function () {
		this.setData({
			scrollTop: 0,
		})
	},
	//share 统一分享
	onShareAppMessage: function (res) {
		if (res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '财咖｜会计招聘找工作',
			path: '/pages/index/index',
			imageUrl: '/images/share.jpg',
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			}
		}
	},
})